package com.lims.landInfo.action;

import javax.servlet.http.HttpServletRequest;


import com.opensymphony.xwork2.Preparable;
import com.opensymphony.xwork2.ModelDriven;


import javacommon.base.*;


import com.lims.landInfo.model.*;
import com.lims.landInfo.service.*;
import com.lims.landInfo.vo.query.*;

/**
 * @author zzh
 * @version 1.0
 * @since 1.0
 */
@SuppressWarnings({ "serial", "unchecked" })
public class LandAction extends BaseStruts2Action implements Preparable,ModelDriven{
	//默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = null; 
	
	//forward paths
	protected static final String LIST_JSP= "/pages/landInfo/Land/list.jsp";
	private static final String LandMap_JSP = "/pages/landInfo/Land/landMap.jsp";
	private static final String LandMapFiish_JSP = "/pages/landInfo/Land/landMapFinish.jsp";
	//redirect paths,startWith: !
	protected static final String LIST_ACTION = "!/pages/landInfo/Land/list.do";
	
	private LandManager landManager;
	
	private Land land;
	java.lang.String id = null;
	private String items;

	public void prepare() throws Exception {
		if (isNullOrEmptyString(id)) {
			land = new Land();
		} else {
			land = (Land)landManager.getById(id);
		}
	}
	
	/** 增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,注意大小写 */
	public void setLandManager(LandManager manager) {
		this.landManager = manager;
	}	
	
	public Object getModel() {
		return land;
	}
	
	public void setObjid(java.lang.String val) {
		this.id = val;
	}

	public void setItems(String items) {
		this.items = items;
	}
	
	/** 进入list页面 */
	public String list() {
		return LIST_JSP;
		
	}
	
	/**执行搜索**/
	public String findList() {
		LandQuery query = newQuery(LandQuery.class,DEFAULT_SORT_COLUMNS);
		ListRange listRange = this.landManager.findRange(query);
		this.responseOutWithJson(listRange);
		return null;
	}
	
	/**
	 * 进入设置坐标页
	 * @return
	 */
	public String sosoMap() {
		HttpServletRequest request = this.getRequest();
		request.setAttribute("land", this.land);
		return LandMap_JSP;
	}
	
	/**
	 * 保存坐标点
	 * @return
	 */
	public String savePosition(){
		HttpServletRequest request = this.getRequest();
		Land mapLand = this.landManager.getById(request.getParameter("objid"));
		mapLand.setLat(request.getParameter("lat"));
		mapLand.setLng(request.getParameter("lng"));
		this.landManager.saveOrUpdate(mapLand);
		return LandMapFiish_JSP;
	}
	
	
	/** 保存新增对象 */
	public String save() {
		landManager.save(land);
		this.responseOutWithJson("{\"success\":true,\"msg\":\"保存成功\"}");
		return null;
	}
	
	/**保存更新对象*/
	public String update() {
		landManager.update(this.land);
		this.responseOutWithJson("{\"success\":true,\"msg\":\"更新成功\"}");
		return null;
	}
	
	/**删除对象*/
	public String delete() {
		String[] objids = items.split(",");
		for (int i = 0; i < objids.length; i++) {		
			landManager.removeById(objids[i]);
		}
		this.responseOutWithJson("{\"success\":true,\"msg\":\"删除成功\"}");
		return null;
	}

}
